package com.imarkket.Dialogs;

        import android.app.Activity;
        import android.app.AlertDialog;
        import android.app.Dialog;
        import android.app.DialogFragment;
        import android.content.DialogInterface;
        import android.os.Bundle;
        import android.text.TextUtils;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.widget.Button;
        import android.widget.CheckBox;
        import android.widget.EditText;

        import com.imarkket.Library.c_logging;
        import com.imarkket.R;

        import java.text.SimpleDateFormat;
        import java.util.Calendar;
        import java.util.TimeZone;

public class c_dialog_NewLista extends DialogFragment {

    public interface ListenerInterface {
        void onSubmitListenerInterface_NewLista(String arg, int Shared);
    }
    public ListenerInterface mListener;

    // Override the Fragment.onAttach() method to instantiate the
    // NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the
            // host
            mListener = (ListenerInterface) activity;

        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement DialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        c_logging.getInstance().log(c_logging.LOG_INFO, getString(R.string.st_sl_Nueva_Lista));

        // Use the Builder class for convenient dialog construction
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Vista del Dialog
        View view = getActivity().getLayoutInflater().inflate(R.layout.l_df_anadir_lista, null);

        builder.setView(view);

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.l_df_anadir_lista, null))
                // Add action buttons

                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // No hacemos nada, override del método onStart y ahí hago todo.
                        // He tenido que hacer esto porque si lo implementaba aquí
                        // Se cerraba el cuadro de dialogo y siempre y no se podía evitar.
                        // si lo hago en onstart ya se cierra cuando quiera
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        c_logging.getInstance().log(c_logging.LOG_INFO, getString(R.string.st_la_No));
                    }
                });

        // Create the AlertDialog object and return it
        return builder.create();
    }

    @Override
    public void onStart() {
        super.onStart();    //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point
        AlertDialog d = (AlertDialog) getDialog();
        if (d != null) {
            Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    c_logging.getInstance().log(c_logging.LOG_INFO, getString(R.string.st_la_Yes));

                    EditText m = (EditText) getDialog().findViewById(R.id.new_list);
                    m.setError(null);

                    CheckBox c = (CheckBox) getDialog().findViewById(R.id.CheckShared);

                    if (TextUtils.isEmpty(m.getText().toString())) {
                        m.setError(getString(R.string.st_la_Field_Required));
                        m.requestFocus();
                    } else {
                        int myInt = (c.isChecked()) ? 1 : 0;
                        c_logging.getInstance().log(c_logging.LOG_INFO, "Nueva lista: ("+m.getText().toString()+")");
                        mListener.onSubmitListenerInterface_NewLista(m.getText().toString(), myInt);
                        dismiss();
                    }
                }
            });


            // Acción al pulsar el checkbox
            CheckBox CheckDefaultName = (CheckBox) d.findViewById(R.id.CheckDefaultName);

            CheckDefaultName.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (((CheckBox) v).isChecked()) {
                        EditText NewListEditText = (EditText) getDialog().findViewById(R.id.new_list);
                        NewListEditText.setEnabled(false);

                        //Getting Current Time
                        Calendar ctaq = Calendar.getInstance();
                        SimpleDateFormat dfaq = new SimpleDateFormat("dd/MM/yyyy");

                        //Getting Current Timezone
                        Calendar cll = Calendar.getInstance();
                        TimeZone z = cll.getTimeZone();
                        NewListEditText.setText("Lista " + dfaq.format(ctaq.getTime()));
                        c_logging.getInstance().log(c_logging.LOG_INFO, "Default List Name: " + NewListEditText.getText());
                    }
                    else {
                        EditText NewListEditBox = (EditText) getDialog().findViewById(R.id.new_list);
                        NewListEditBox.setEnabled(true);
                    }
                }
            });

        }
    }
}
